package pers.zwy.DMSystem.dormitoryManagement.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import pers.zwy.DMSystem.dormitoryManagement.dao.DormitoryDao;
import pers.zwy.DMSystem.dormitoryManagement.domain.Dormitory;
import pers.zwy.DMSystem.util.dao.impl.BaseDaoImpl;

public class DormitoryDaoImpl extends BaseDaoImpl implements DormitoryDao {

	public String getInsertSql() {
		return "insert into t_dormitory (d_id,r_id,already,max)values(?,?,?,?)";
	}

	public void setInsertSql(Object data, PreparedStatement pstm) throws Exception {
		Dormitory d = (Dormitory)data;
		pstm.setInt(1, d.getD_id());
		pstm.setInt(2, d.getR_id());
		pstm.setInt(3, d.getAlready());
		pstm.setInt(4, d.getMax());
	}

	public String getDeleteSql() {
		return "delete from t_dormitory where id=?";
	}

	public void setDeleteSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);
	}

	public String getUpdateSql() {
		return "update t_dormitory set d_id=?,r_id=?,already=?,max=? where id=?";
	}

	public void setUpdateSql(Object data, PreparedStatement pstm) throws Exception {
		Dormitory d = (Dormitory)data;
		pstm.setInt(1, d.getD_id());
		pstm.setInt(2, d.getR_id());
		pstm.setInt(3, d.getAlready());
		pstm.setInt(4, d.getMax());
		pstm.setInt(5, d.getId());
	}

	public String getFindByIdSql() {
		return "select *from t_dormitory where id=?";
	}

	public void setFindByIdSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);
	}

	public String getFindAllSql() {
		return "select *from t_dormitory";
	}

	public String getFindByConditionSql(Object data) throws Exception {
		Dormitory dormitory = (Dormitory)data;
		StringBuilder sql = new StringBuilder("select *from t_dormitory where 1=1");
		if(dormitory.getId()!=null){
			sql.append(" and id="+dormitory.getId());
		}
		if(dormitory.getD_id()!=null){
			sql.append(" and d_id="+dormitory.getD_id());
		}
		if(dormitory.getR_id()!=null){
			sql.append(" and r_id="+dormitory.getR_id());
		}
		if(dormitory.getAlready()!=null){
			sql.append(" and already="+dormitory.getAlready());
		}
		if(dormitory.getMax()!=null){
			sql.append(" and max="+dormitory.getMax());
		}
		return new String(sql);
	}

	public Object rsToObject(ResultSet rs) throws Exception {
		Dormitory dormitory = new Dormitory();
		dormitory.setId(rs.getInt("id"));
		dormitory.setD_id(rs.getInt("d_id"));
		dormitory.setR_id(rs.getInt("r_id"));
		dormitory.setAlready(rs.getInt("already"));
		dormitory.setMax(rs.getInt("max"));
		return dormitory;
	}

}
